﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.BAL.Common;
using System.Net.Mail;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.IO;

namespace OrganizedIN
{

    public partial class AjaxRegistration : System.Web.UI.Page
    {

        public string FrstName, LstName, Gender, BirthDay, email, Tzone, password, Repassword, TrmCond, Orgnz, comments, ActiveKey;
        public string SysMailID, ToMail, MailPassword;
        SqlService objservices = new SqlService();
        protected void Page_Load(object sender, EventArgs e)
        {
            FrstName = (Request.QueryString["FrstName"] != null) ? Request.QueryString["FrstName"].ToString() : null;
            LstName = (Request.QueryString["LstName"] != null) ? Request.QueryString["LstName"].ToString() : null;
            Gender = (Request.QueryString["Gender"] != null) ? Request.QueryString["Gender"].ToString() : null;
            BirthDay = (Request.QueryString["BirthDay"] != null) ? Request.QueryString["BirthDay"].ToString() : null;
            email = (Request.QueryString["email"] != null) ? Request.QueryString["email"].ToString() : null;
            Tzone = (Request.QueryString["Tzone"] != null) ? Request.QueryString["Tzone"].ToString() : null;
            password = (Request.QueryString["password"] != null) ? Request.QueryString["password"].ToString() : null;
            TrmCond = (Request.QueryString["TrmCond"] != null) ? Request.QueryString["TrmCond"].ToString() : null;
            Orgnz = (Request.QueryString["Orgnz"] != null) ? Request.QueryString["Orgnz"].ToString() : null;
            comments = (Request.QueryString["comments"] != null) ? Request.QueryString["comments"].ToString() : null;

            try
            {

                BLL registerobj = new BLL();
                UserRegistration setdata = new UserRegistration();
                DataSet Ds = new DataSet();
                Ds = Common.Get_ActivationKey();
                ActiveKey = Ds.Tables[0].Rows[0][0].ToString();
                if (FrstName != "" && password != "" && TrmCond != "" && Gender != "" && BirthDay != "")
                {

                    string url = "confirmation.aspx";
                    setdata.ActivationKey = ActiveKey;
                    setdata.UserEmailID = email;
                    setdata.UserFname = FrstName;
                    setdata.UserLname = LstName;
                    setdata.UserGender = Gender;
                    setdata.UserDOB = BirthDay;
                    setdata.UserPassword = password;
                    setdata.UserTimeZone = Tzone;
                    setdata.UserRegistrationDate = System.DateTime.Now;
                    setdata.UserHearAbtOrganizedin = Orgnz;
                    setdata.UserAddComment = comments;
                    registerobj.UserRegistrationInsert(setdata);
                    ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>OpenConfirmWin('" + url + "')</script>");
                    Sendmail();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Sendmail()
        {
            SysMailID = ConfigurationManager.AppSettings.Get("SystemMailID");
            MailPassword = ConfigurationManager.AppSettings.Get("MailPassword");

            MailMessage objMail = new MailMessage();
            objMail.To.Add(email);
            objMail.From = new MailAddress(SysMailID);
            objMail.Subject = "Email using Gmail";
            long AuthenticateNo = getRandomNo();
            string text = File.ReadAllText(Server.MapPath("~/AuthenticationTemplate.htm"));
            string Body = text.Replace("##FirstName##", FrstName).Replace("##UserName##", email).Replace("##AuthenticationCode##", AuthenticateNo.ToString()).Replace("##AuthenticationLink##", ConfigurationManager.AppSettings["SiteLink"] + "/UI/Authentiction.aspx?id=" + email).Replace("##Logo##", ConfigurationManager.AppSettings["SiteLink"]);

            //string Body = "Hi, this mail is to test sending ,  Please click on belowlink for activate Your account for 60 days trail period. <a href='www.womenartfoundation.com/Test.aspx?Activation='"+ActiveKey;
            objMail.Body = Body;
            objMail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings.Get("SMTP");
            smtp.Credentials = new System.Net.NetworkCredential(SysMailID, MailPassword);
            smtp.EnableSsl = true;
            try
            {

                ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>alert('Please check your email ID for confirmation')</script>");
                Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, "insert into User_Authentication values('" + email + "'," + AuthenticateNo + ")");
                smtp.Send(objMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private long getRandomNo()
        {
            BLL registerobj = new BLL();
            Random rndNo = new Random();
            long autheNo = rndNo.Next(1000, 9999);
            DataSet ds = registerobj.Authentication();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (autheNo == Convert.ToInt64(ds.Tables[0].Rows[i]["AuthenticateNo"].ToString()))
                    {
                        getRandomNo();
                    }
                }
            }
            return (autheNo);
        }
    }
}